import {
  ApiItemTypeMapping,
  EventRecommendationShape,
} from "@/data/shared/entityShapes";
import { partition } from "lodash-es";
import { useEffect, useState } from "react";

const TO_FILTER = [ApiItemTypeMapping.Exhibition, ApiItemTypeMapping.Talk];

export const useEventListSplit = (eventRecommendationParentObj: {
  eventRecommendations?: EventRecommendationShape[];
}) => {
  const [eventRecSplit, setEventRecSplit] = useState<{
    courses: EventRecommendationShape[];
    otherEvents: EventRecommendationShape[];
  }>({
    courses: [],
    otherEvents: [],
  });

  useEffect(() => {
    if (eventRecommendationParentObj) {
      const [exhibiionTalksArray, courseArray] = partition(
        eventRecommendationParentObj.eventRecommendations,
        (event) => TO_FILTER.includes(event.typeCourse)
      );

      setEventRecSplit({
        courses: courseArray,
        otherEvents: exhibiionTalksArray,
      });
    }
  }, [eventRecommendationParentObj]);

  return eventRecSplit;
};
